package child;

import java.awt.Shape;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.ArrayList;
import java.util.Map;
import java.util.TreeMap;

import org.jhotdraw.draw.Figure;
import org.jhotdraw.samples.pert.figures.TaskFigure;

import senders.BroadCaster;
import senders.ChildBroadcaster;
import senders.IBroadcaster;
import sr.logger.Logger;
import util.FigureWrapper;

import client.remote.ClientReceiver;
import main.GateComm;
import main.GateTaskFactory;
import main.Server;
import master.ChildInterface;

public class ChildServer extends UnicastRemoteObject implements ChildInterface 
{
	Logger logger = Logger.getLogger(this.getClass());
	ArrayList<ClientReceiver> clients;
	ChildBroadcaster broadcaster;
	static final int MAX_ERROR_COUNT = 5;
	GateComm gateComm;
	Map<ClientReceiver,Integer> errors = new TreeMap<ClientReceiver, Integer>();
	private Server server;
	int roomId;
	public ChildServer(ArrayList<ClientReceiver> clients, Server server) throws RemoteException
	{
		this.server = server;
		this.clients = clients;
		broadcaster = new ChildBroadcaster(this);
		gateComm = GateComm.getComm();
		gateComm.addTask(GateTaskFactory.createNewRoomTask(createPlayersIdList(),0));
		roomId = server.getCurrentRoomCounter();
	}
	//with set room Id
	public ChildServer(ArrayList<ClientReceiver> clients, Server server,int roomId) throws RemoteException
	{
		this.server = server;
		this.clients = clients;
		broadcaster = new ChildBroadcaster(this);
		gateComm = GateComm.getComm();
		gateComm.addTask(GateTaskFactory.createNewRoomTask(createPlayersIdList(),0));
		roomId = this.roomId;
	}
	private ArrayList<Long> createPlayersIdList()
	{
		ArrayList<Long> gamerIds = new ArrayList<Long>();
		for(ClientReceiver client : clients)
		{
			Long id = server.getIdByReceiver(client);
			if(id != -1)
				gamerIds.add(id);
		}
		return gamerIds;
	}
	public ArrayList<ClientReceiver> getClients()
	{
		return clients;
	}
	@Override
	public void addFigure(Shape shape,Long tick) throws RemoteException {
		logger.log(tick,"AddFigure");
		broadcaster.bcFigure(shape);
	}

	@Override
	public void addMessage(String msg,Long tick) throws RemoteException {
		
		logger.log(tick,"addMessage");
		broadcaster.bcMessage(msg);
		System.out.println("Child got msg");
	}

	@Override
	public void disconnect(ClientReceiver clientReceiver,Long tick)
			throws RemoteException 
	{
		logger.log(tick,"disconnect");
		clients.remove(clientReceiver);
		if(clients.isEmpty())
		{
			broadcaster.shutdown();
			UnicastRemoteObject.unexportObject(this,true);
		}
		gateComm.addTask(GateTaskFactory.createClientDisconnectRoomTask(roomId,server.getIdByReceiver(clientReceiver)));
	}
	public void reportError(ClientReceiver client)
	{
		Integer errorCount = errors.get(client);
		errors.put(client, ++errorCount);
		if(errorCount > MAX_ERROR_COUNT)
		{
			errors.remove(client);
			try
			{
				disconnect(client,logger.log("reportError"));
			} catch (RemoteException e)
			{
				e.printStackTrace();
			}
		}
			
	}
}
